package cn.ubeta.b2c.bean;

import android.text.TextUtils;

import cn.ubeta.b2c.DemoData;

import java.util.ArrayList;
import java.util.List;

import static cn.ubeta.b2c.DemoData.price;

/**
 * Created by liudengyong on 16/8/17.
 */

public class CartInfo extends ApiResp {

    private List<CartProductInfo> productList;

    /**
     * 字符串反序列化
     * @param value
     */
    public CartInfo(String value) {
        if (!TextUtils.isEmpty(value)) {
            String[] token = value.split(";");
            if (token != null && token.length > 0) {
                productList = new ArrayList<>();
                for (String item : token) {
                    String[] t2 = item.split(",");
                    int skuId = Integer.parseInt(t2[0]);
                    String name = t2[1];
                    String desc = t2[2];
                    int price = Integer.parseInt(t2[3]);
                    int count = Integer.parseInt(t2[4]);
                    boolean checked = Integer.parseInt(t2[5]) != 0;
                    productList.add(new CartProductInfo(skuId, name, desc, price, count, checked));
                }
            }
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (productList != null) {
            for (CartProductInfo info : productList) {
                if (sb.length() == 0)
                    sb.append(info.toString());
                else
                    sb.append(";" + info.toString());
            }
        }
        return sb.toString();
    }

    public static class CartProductInfo extends ProductInfo {
        private int count;
        private boolean checked;

        public CartProductInfo(long skuId, String name, String desc, double price, int count, boolean checked) {
            super(skuId, name, desc, price);
            this.count = count;
            this.checked = checked;
        }

        public CartProductInfo(ProductInfo info, int count) {
            this(info.skuId, info.name, info.desc, info.getPriceInfo().getPrice(), count, true);
        }

        @Override
        public String toString() {
            return String.format("%d,%s,%s,%d,%d,%d", skuId, name, desc, price, count, checked ? 1 : 0);
        }

        public int getCount() {
            return count;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }

        public boolean isChecked() {
            return checked;
        }

        public static List<CartProductInfo> getTestData() {
            CartInfo info = DemoData.getCartInfo();
            if (info != null)
                return info.getProductList();
            return null;
        }
    }

    public List<CartProductInfo> getProductList() {
        return productList;
    }

    /**
     * 获取购物车个数
     * @return
     */
    public int getCount() {
        int count = 0;
        if (productList != null) {
            for (CartProductInfo info : productList) {
                count += info.count;
            }
        }
        return count;
    }

    public void addCart(ProductInfo info) {
        if (productList == null)
            productList = new ArrayList<>();

        for (CartProductInfo item : productList) {
            if (item.getSkuId() == info.getSkuId()) {
                item.count++;
                return;
            }
        }

        productList.add(new CartProductInfo(info, 1));
    }
}
